# 直方图比较
def create_rgb_hist(image):
    h, w, c = image.shape
    rgbHist = np.zeros([16*16*16, 1], np.float32)
    bsize = 256/16
    # enumerate() 函数可以永健一个可遍历的数据对象（如列表、元组或字符串）组合为一个索引序列，同时列出数据以及对应的下标，一般用在for循环中。
    # range()函数用于创建一个整数列表
    for row in range(h):
        for col in range(w):
            b = image[row, col, 0]
            g = image[row, col, 1]
            r = image[row, col, 2]
            index = np.int((b/bsize)/16*16 + (g/bsize)*16 + (r/bsize))
            rgbHist[np.int(index), 0] = rgbHist[np.int(index), 0] + 1

    return rgbHist


def hist_compare(image1, image2):
    hist1 = create_rgb_hist(image1)
    hist2 = create_rgb_hist(image2)
    match1 = cv.compareHist(hist1, hist2, cv.HISTCMP_BHATTACHARYYA)
    match2 = cv.compareHist(hist1, hist2, cv.HISTCMP_CORREL)
    match3 = cv.compareHist(hist1, hist2, cv.HISTCMP_CHISQR)
    print("巴氏距离: %s, 相关性: %s, 卡方: %s"%(match1, match2, match3))
    cv.imshow("image1", image1)
    cv.imshow("image2", image2)


image1 = cv.imread("./images/raindropGirl.jpg")
image2 = cv.imread("./images/raindropGirl01.jpg")

hist_compare(image1, image2)
cv.waitKey(0)
cv.destroyAllWindows()
